<template>
  <div class='container'>
    <el-dialog :visible="addDialogFn" :title="titleFn" @close="closeDialogFn" :close-on-click-modal="false">
      <el-form ref="addForm" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="文章标题:" prop="title" label-width="100px">
        <el-input v-model="form.title" placeholder="请输入文章标签"  />
      </el-form-item>
      <el-form-item label="文章内容:" prop="articleBody" label-width="100px">
        <!-- 富文本编辑器 -->
        <quill-editor
          :options="editorOption"
          v-model="form.articleBody"
          @blur="quillEditorRules"
        ></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址:" prop="description" label-width="100px">
        <el-input v-model="form.videoURL" placeholder="请输入视频地址" />
      </el-form-item>
      <el-form-item style="text-align: right">
        <el-button @click="closeDialogFn">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </el-form-item>
    </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { add, update, detail } from '@/api/hmmm/articles.js'

const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote', 'code-block'], // 引用 代码块-----['blockquote', 'code-block']
  ['image', 'link'] // 链接、图片、视频-----['link', 'image', 'video']
]
export default {
  data () {
    return {
      // 富文本编辑器的配置
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      },
      titleFn: '',
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
        // id: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  props: {
    addDialogFn: {
      type: Boolean,
      default: false
    }
  },
  components: {
    quillEditor
  },
  methods: {
    closeDialogFn () {
      this.$emit('closeDialogFn')
    },
    quillEditorRules () {
      this.$refs.addForm.validateField('articleBody')
    },
    confirmAdd () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改成功')
        } else {
          await add(this.form)
          this.$message.success('新增成功')
        }
        this.closeDialogFn()
        this.$parent.$parent.getList()
        this.clearform()
      })
    },
    clearform () {
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
        // id: null
      }
      this.$refs.addForm.resetFields()
    },
    // 回填文章
    async preview (data) {
      // this.$emit('openadddialog')
      const res = await detail(data)
      console.log(res)
      this.form = res.data
    }

  }
}
</script>

<style scoped lang='scss'>
.el-form-item::v-deep .ql-editor {
  height: 200px;
}</style>
